/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.vincent.filepickersample;

import com.vincent.filepicker.ability.AudioPickAbility;
import com.vincent.filepicker.ability.ImagePickAbility;
import com.vincent.filepicker.ability.NormalFilePickAbility;
import com.vincent.filepicker.ability.VideoPickAbility;
import junit.framework.TestCase;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.aafwk.ability.delegation.IAbilityDelegator;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.app.Context;
import ohos.miscservices.timeutility.Time;
import ohos.multimodalinput.event.TouchEvent;
import org.junit.Assert;
import org.junit.Test;

public class ExampleOhosTest extends TestCase {

    private IAbilityDelegator mIAbilityDelegator = AbilityDelegatorRegistry.getAbilityDelegator();
    private Context mContext;

    @Override
    protected void setUp() throws Exception {
        mContext = mIAbilityDelegator.getAppContext();
        Thread.sleep(3000L);
    }

    @Override
    protected void tearDown() throws Exception {
        Thread.sleep(2000);
        EventHelper.clearAbilities();
    }

    private void simulateClickOnScreen(Ability ability, Component component) {
        int[] pos = component.getLocationOnScreen();
        pos[0] += component.getWidth() / 2;
        pos[1] += component.getHeight() / 2;

        simulateClickOnScreen(ability, pos[0], pos[1]);
    }

    private void simulateClickOnScreen(Ability ability, int x, int y) {
        long downTime = Time.getRealActiveTime();
        long upTime = downTime;
        TouchEvent ev1 = EventHelper.obtainTouchEvent(downTime, downTime, EventHelper.ACTION_DOWN, x, y);
        mIAbilityDelegator.triggerTouchEvent(ability, ev1);
        TouchEvent ev2 = EventHelper.obtainTouchEvent(downTime, upTime, EventHelper.ACTION_UP, x, y);
        mIAbilityDelegator.triggerTouchEvent(ability, ev2);
    }

    @Test
    public void testChooseImage() throws InterruptedException {
        ImagePickAbility ability = EventHelper.startAbility(ImagePickAbility.class);
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_rv_image_pick);
        Component item = listContainer.getComponentAt(0);
        Component selectComponent = item.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent);
        Thread.sleep(2000L);
        Component item1 = listContainer.getComponentAt(1);
        Component selectComponent1 = item1.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent1);
        Thread.sleep(2000L);
        Component item2 = listContainer.getComponentAt(2);
        Component selectComponent2 = item2.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent2);
        Thread.sleep(2000L);
        Assert.assertTrue(ability.mSelectedList.size() == 3);
    }

    @Test
    public void testChooseVideo() throws InterruptedException {
        ImagePickAbility ability = EventHelper.startAbility(VideoPickAbility.class);
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_rv_image_pick);
        Component item = listContainer.getComponentAt(0);
        Component selectComponent = item.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent);
        Thread.sleep(2000L);
        Component item1 = listContainer.getComponentAt(1);
        Component selectComponent1 = item1.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent1);
        Thread.sleep(2000L);
        Assert.assertTrue(ability.mSelectedList.size() == 2);
    }

    @Test
    public void testChooseAudio() throws InterruptedException {
        ImagePickAbility ability = EventHelper.startAbility(AudioPickAbility.class);
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_rv_image_pick);
        Component item = listContainer.getComponentAt(0);
        Component selectComponent = item.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent);
        Thread.sleep(2000L);
        Assert.assertTrue(ability.mSelectedList.size() == 1);
    }

    @Test
    public void testChooseFile() throws InterruptedException {
        ImagePickAbility ability = EventHelper.startAbility(NormalFilePickAbility.class);
        ListContainer listContainer = (ListContainer) ability.findComponentById(ResourceTable.Id_rv_image_pick);
        Component item = listContainer.getComponentAt(0);
        Component selectComponent = item.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent);
        Thread.sleep(2000L);
        Component item1 = listContainer.getComponentAt(1);
        Component selectComponent1 = item1.findComponentById(ResourceTable.Id_cbx);
        simulateClickOnScreen(ability, selectComponent1);
        Thread.sleep(2000L);
        Assert.assertTrue(ability.mSelectedList.size() == 2);
    }
}